<template>
  <div
    v-loading.fullscreen.lock="loading"
    class="site-wrapper"
    :class="{ 'site-sidebar--fold': sidebarFold }"
    element-loading-text="拼命加载中"
  >
    <template v-if="!loading">
      <main-navbar />
      <main-sidebar />
      <div class="site-content__wrapper" :style="{ 'min-height': documentClientHeight + 'px' }">
        <main-content v-if="!$store.state.common.contentIsNeedRefresh" />
      </div>
    </template>
  </div>
</template>

<script>
import MainNavbar from './main-navbar'
import MainSidebar from './main-sidebar'
import MainContent from './main-content'
export default {
  provide() {
    return {
      // 刷新
      refresh() {
        this.$store.commit('common/updateContentIsNeedRefresh', true)
        this.$nextTick(() => {
          this.$store.commit('common/updateContentIsNeedRefresh', false)
        })
      }
    }
  },
  components: {
    MainNavbar,
    MainSidebar,
    MainContent
  },
  data() {
    return {
      loading: true
    }
  },
  computed: {
    documentClientHeight: {
      get() { return this.$store.state.common.documentClientHeight },
      set(val) { this.$store.commit('common/updateDocumentClientHeight', val) }
    },
    sidebarFold: {
      get() { return this.$store.state.common.sidebarFold }
    },
    userId: {
      get() { return this.$store.state.user.id },
      set(val) { this.$store.commit('user/updateId', val) }
    },
    userName: {
      get() { return this.$store.state.user.name },
      set(val) { this.$store.commit('user/updateName', val) }
    },
    shopId: {
      get() { return this.$store.state.user.shopId },
      set(val) { this.$store.commit('user/updateShopId', val) }
    },
    userPhoto: {
      get() { return this.$store.state.user.photo },
      set(val) { this.$store.commit('user/updatePhoto', val) }
    }
  },
  created() {
    this.getUserInfo()
  },
  mounted() {
    this.resetDocumentClientHeight()
  },
  methods: {
    // 重置窗口可视高度
    resetDocumentClientHeight() {
      this.documentClientHeight = document.documentElement['clientHeight']
      this.$store.commit('common/updateDocumentClientDefaultHeight', this.documentClientHeight)
      window.onresize = () => {
        this.documentClientHeight = document.documentElement['clientHeight']
        this.$store.commit('common/updateDocumentClientDefaultHeight', this.documentClientHeight)
      }
    },
    // 获取当前管理员信息
    getUserInfo() {
      this.$http({
        url: this.$http.adornUrl('/user/info'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.loading = false
          this.userId = data.user.staffid
          this.userName = data.user.loginname
          this.userPhoto = data.user.staffphoto
          this.shopId = data.user.shopId
        }
      })
    }
  }
}
</script>
